package com.tql.mall.boss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tql.mall.boss.mapper.BaseAttrInfoMapper;
import com.tql.mall.boss.service.BaseAttrInfoService;
import com.tql.mall.boss.service.BaseAttrValueService;
import com.tql.mall.model.product.BaseAttrInfo;
import com.tql.mall.model.product.BaseCategoryView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    //待修改
    @Override
    public List<BaseAttrInfo> getListByBaseCategoryView(BaseCategoryView baseCategoryView) {
        QueryWrapper<BaseAttrInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_level",1).eq("category_id",baseCategoryView.getCategory1Id())
                .or().eq("category_level",2).eq("category_id",baseCategoryView.getCategory2Id())
                        .or().eq("category_level",3).eq("category_id",baseCategoryView.getCategory3Id());
        /**
         * select * from
         * table
         * where category_level = 1 and category_id = baseCategoryView.category1Id
         * or
         * category_level = 2 and category_id = baseCategoryView.category2Id
         * or
         * category_level = 3 and category_id = baseCategoryView.category3Id
         */
        List<BaseAttrInfo> list = baseMapper.selectList(queryWrapper);
        //查询list下所有的商品属性的属性值
        list.stream().forEach(item ->{
            item.setAttrValueList(baseAttrValueService.getAttrValueListByAttrId(item.getId()));
        });
        return list;

    }

    @Override
    public List<BaseAttrInfo> getAttrListByLevelAndId(Integer level, Long id) {
        QueryWrapper<BaseAttrInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_level",level)
                        .eq("category_id",id);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BaseAttrInfo> getAttrList(Long category3Id) {
        QueryWrapper<BaseAttrInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_level",3)
                        .eq("category_id",category3Id);
        //查询每个属性的具体属性值
        List<BaseAttrInfo> list = baseMapper.selectList(queryWrapper);
        list.stream().forEach(item ->{
            item.setAttrValueList(baseAttrValueService.getAttrValueListByAttrId(item.getId()));
        });
        return list;
    }
}
